﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MINERP.MODEL.Monitor;
using MINERP.BLL.Monitor;
using MINERP.ComControls;

namespace MINERP.Monitor
{
    public delegate void ClikEventHander(object sender, EventArgs e);
    public partial class FrmMain : FrmParent
    {
        private int _userid;
        public ClikEventHander clikEventHander;
        private MonitorBLL bll = new MonitorBLL();
        DateTime start = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 0, 0, 0);
        DateTime end = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 23, 59, 59);
        //DateTime start = new DateTime(DateTime.Now.Year, 10, DateTime.Now.Day, 0, 0, 0);
        //DateTime end = new DateTime(DateTime.Now.Year, 10, DateTime.Now.Day, 23, 59, 59);
        public FrmMain()
        {
            InitializeComponent();
            base.InitiAuthority();
        }

        private void FrmMain1_Load(object sender, EventArgs e)
        {
            this._userid = base.userId;
            tsDateTime.Text = DateTime.Now.ToString();
            clikEventHander = new ClikEventHander(ucCustomer_Click);
            try
            {
                Cursor = Cursors.WaitCursor;
                BindMonitor();
                Cursor = Cursors.Default;
            }
            catch (Exception ex)
            {
                Cursor = Cursors.Default;
                MessageBox.Show(ex.Message, "确认", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }

        }

        void ucCustomer_Click(object sender, EventArgs e)
        {
            Control ctrl = ((Control)sender).Parent.Parent;

            if (ctrl is UCCustomer)
            {
                FrmTaskDetailCustomer frmTaskDetailCustomer = new FrmTaskDetailCustomer();
                frmTaskDetailCustomer._techid = ((UCCustomer)ctrl).TechId;
                frmTaskDetailCustomer._priority = Convert.ToInt32(cbPriority.Text);
                frmTaskDetailCustomer._start = start;
                frmTaskDetailCustomer._end = end;
                frmTaskDetailCustomer.ShowDialog();
            }
            if (ctrl is UCMidTech)
            {
                FrmTaskDetailMidTech frmTaskDetailMidTech = new FrmTaskDetailMidTech();
                frmTaskDetailMidTech._techid = ((UCMidTech)ctrl).TechId;
                frmTaskDetailMidTech._priority = Convert.ToInt32(cbPriority.Text);
                frmTaskDetailMidTech._start = start;
                frmTaskDetailMidTech._end = end;
                frmTaskDetailMidTech.ShowDialog();
            }
            if (ctrl is UCDeliverGoods)
            {
                FrmTaskDetailDelively frmTaskDetailDelively = new FrmTaskDetailDelively();
                frmTaskDetailDelively._techid = ((UCDeliverGoods)ctrl).TechId;
                frmTaskDetailDelively._priority = Convert.ToInt32(cbPriority.Text);
                frmTaskDetailDelively._start = start;
                frmTaskDetailDelively._end = end;
                frmTaskDetailDelively.ShowDialog();
            }

        }

        #region 绑定监控数据源
        /// <summary>
        /// 绑定监控数据源
        /// </summary>
        private void BindMonitor()
        {

            List<MODEL.Monitor.MonitorInfo> monitors = bll.GetMonitorInfo(start,end, Convert.ToInt16(cbPriority.Text));
            foreach (var item in monitors)
            {
                switch (item.TechName)
                {
                    case "客服":
                        ucCustomer.TechId = item.TechId;
                        ucCustomer.TechName = item.TechName;
                        ucCustomer.BindData(item.CompletedTask.ToString(), item.CompletedArea.ToString(), item.PriorityTaskNoComplete.ToString(),item.PriorityAreaNoComplete.ToString());
                        break;
                    case "调色":
                        ucMidTechTiaoSe.TechId = item.TechId;
                        ucMidTechTiaoSe.TechName = item.TechName;
                        ucMidTechTiaoSe.BindData(item.CompletedArea.ToString(), item.CompletedTask.ToString(), item.NoCompleteArea.ToString(),
                            item.PriorityArea.ToString(), item.NoCompleteTask.ToString(), item.PriorityTask.ToString());
                        break;
                    case "打印管理":
                        ucMidTechFenPeiDaYinJi.TechId = item.TechId;
                        ucMidTechFenPeiDaYinJi.TechName = item.TechName;
                        ucMidTechFenPeiDaYinJi.BindData(item.CompletedArea.ToString(), item.CompletedTask.ToString(), item.NoCompleteArea.ToString(),
                            item.PriorityArea.ToString(), item.NoCompleteTask.ToString(), item.PriorityTask.ToString());
                        break;
                    case "输出管理":

                        //ucMidTechTiaoSe.TechName = item.TechName;
                        //ucMidTechTiaoSe.BindData(item.CompletedArea.ToString(), item.CompletedTask.ToString(), item.NoCompleteArea.ToString(),
                        //    item.PriorityArea.ToString(), item.NoCompleteTask.ToString(), item.PriorityTask.ToString());
                        break;
                    case "分片":
                        ucMidTechFenPian.TechId = item.TechId;
                        ucMidTechFenPian.TechName = item.TechName;
                        ucMidTechFenPian.BindData(item.CompletedArea.ToString(), item.CompletedTask.ToString(), item.NoCompleteArea.ToString(),
                            item.PriorityArea.ToString(), item.NoCompleteTask.ToString(), item.PriorityTask.ToString());
                        break;
                    case "委外":
                        ucMidTechWeiWan.TechId = item.TechId;
                        ucMidTechWeiWan.TechName = item.TechName;
                        ucMidTechWeiWan.BindData(item.CompletedArea.ToString(), item.CompletedTask.ToString(), item.NoCompleteArea.ToString(),
                            item.PriorityArea.ToString(), item.NoCompleteTask.ToString(), item.PriorityTask.ToString());
                        break;
                    case "分堆":
                        ucMidTechFenDui.TechId = item.TechId;
                        ucMidTechFenDui.TechName = item.TechName;
                        ucMidTechFenDui.BindData(item.CompletedArea.ToString(), item.CompletedTask.ToString(), item.NoCompleteArea.ToString(),
                            item.PriorityArea.ToString(), item.NoCompleteTask.ToString(), item.PriorityTask.ToString());
                        break;
                    case "发货":
                        ucDeliverGoods.TechId = item.TechId;
                        ucDeliverGoods.TechName = item.TechName;
                        ucDeliverGoods.BindData(item.NoDeliverTask.ToString(),item.DeliverredTask.ToString());
                        break;
                }
            }

            #region ADD by jw
            lbNoCompleteArea.Text = bll.GetNoCompletedArea().ToString();
            lbNoCompletedProductNum.Text = bll.GetNoCompletedProductNum().ToString();
            lbCompleteArea.Text = bll.GetCompletedArea(start, end).ToString();
            lbCompletedProductNum.Text = bll.GetCompletedProductNum(start, end).ToString();
            lbNoCompleteAreaPriority.Text = bll.GetNoCompletedAreaPriority(Convert.ToInt32(cbPriority.Text)).ToString();
            lbNoCompletedProductNumPriority.Text = bll.GetNoCompletedProductNumPriority(Convert.ToInt32(cbPriority.Text)).ToString();

            #endregion
        }
        #endregion

        private void btnOK_Click(object sender, EventArgs e)
        {
            try
            {
                Cursor = Cursors.WaitCursor;
                BindMonitor();
                Cursor = Cursors.Default;
            }
            catch (Exception ex)
            {
                Cursor = Cursors.Default;
                MessageBox.Show(ex.Message, "确认", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            try
            {
                Cursor = Cursors.WaitCursor;
                BindMonitor();
                Cursor = Cursors.Default;
            }
            catch (Exception ex)
            {
                Cursor = Cursors.Default;
                MessageBox.Show(ex.Message, "确认", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        private void timer2_Tick(object sender, EventArgs e)
        {
            tsDateTime.Text = DateTime.Now.ToString();
        }

        private void tsChat_Click(object sender, EventArgs e)
        {
            frmMain frmMain = frmMain.GetInstance(this._userid);
            frmMain.Show();
           
        }

        private void dtSrart_EditValueChanged(object sender, EventArgs e)
       {
            if (dtSrart.DateTime.ToString() == "0001-1-1 0:00:00")
            {
                start = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 0, 0, 0);
            }
            else
            {
                start = dtSrart.DateTime;
            }
        }

        private void dtEnd_EditValueChanged(object sender, EventArgs e)
        {

            if (dtEnd.DateTime.ToString() == "0001-1-1 0:00:00")
            {
                end = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 0, 0, 0);
            }
            else
            {
                end = dtEnd.DateTime;
            }
        }

    }
}
